using System;
using System.Collections.Generic;
using System.Linq;
using System.Xml.Linq;

namespace WB.Mvc.Forms
{
	/// <summary>
	/// Represents a grouping of fields.
	/// </summary>
	[Serializable]
	public class FieldGroup : EntityWithAttributes
	{
		private readonly List<Field> fields;
		private XElement legend = new XElement("legend");

		/// <summary>
		/// Creates an instance of <see cref="FieldGroup"/>.
		/// </summary>
		public FieldGroup(params Field[] fields)
		{
			this.fields = new List<Field>();
			this.fields.AddRange(fields);
		}

		/// <summary>
		/// Name of the field group.
		/// </summary>
		public string Name
		{
			get { return Attributes["name"]; }
			set { Attributes["name"] = value; }
		}

		public virtual XElement Legend
		{
			get { return legend; }
			set { legend = value; }
		}

		/// <summary>
		/// Get fields belonging to this fieldset.
		/// </summary>
		public virtual IList<Field> Fields
		{
			get { return fields; }
		}

		public Field GetField(string fieldName)
		{
			return Fields.SingleOrDefault(x => x.Name.Equals(fieldName, StringComparison.InvariantCultureIgnoreCase));
		}
	}
}